using System.Drawing.Drawing2D;

namespace 车速仪表盘
{
    public partial class Form1 : Form
    {
        private PictureBox pictureBox;

        public Form1()
        {
            this.Width = 1000;
            this.Height = 800;
            pictureBox = new PictureBox
            {
                Dock = DockStyle.None,
                SizeMode = PictureBoxSizeMode.Zoom,
                Width = 800,
                Height = 600
            };
            this.Controls.Add(pictureBox);
            this.Load += SpeedDashboard_Load;
        }

        private void SpeedDashboard_Load(object sender, EventArgs e)
        {
            DrawDashboard();
        }

        private void DrawDashboard(int speed1 = 0, int speed2 = 180, int speed3 = 158, int meter1 = 2094, int meter2 = 2094, int allMeter = 55986, int remainMeter = 65846, int stopNum = 4, int stopTime = 19, string remark = "")
        {
            int width = 800;
            int height = 550;

            Bitmap bitmap = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            // 设置背景色
            graphics.Clear(Color.Black);

            // 字体设置
            Font largeFont = new Font("Microsoft YaHei", 48, FontStyle.Bold);
            Font mediumFont = new Font("Microsoft YaHei", 36, FontStyle.Bold);
            Font smallFont = new Font("Microsoft YaHei", 24, FontStyle.Bold);
            // 绘制文字
            graphics.DrawString(speed1.ToString(), mediumFont, Brushes.Red, new PointF(420, 35));
            graphics.DrawString(speed2.ToString(), mediumFont, Brushes.Yellow, new PointF(420, 100));
            graphics.DrawString(speed3.ToString(), mediumFont, Brushes.SpringGreen, new PointF(420, 165));
            graphics.DrawString(meter1.ToString(), largeFont, Brushes.White, new PointF(540, 40));
            graphics.DrawString(meter2.ToString(), largeFont, Brushes.Red, new PointF(540, 120));

            DrawSpeedometer(graphics, 300, 300);
            // 中间分割线
            Pen redPen = new Pen(Color.Red, 3);
            graphics.DrawLine(redPen, new Point(0, 240), new Point(width, 240));

            // 绘制下半部分的文字
            graphics.DrawString("累计:", mediumFont, Brushes.Yellow, new PointF(30, 260));
            graphics.DrawString(allMeter.ToString(), largeFont, Brushes.White, new PointF(160, 250));
            graphics.DrawString("剩余:", mediumFont, Brushes.Yellow, new PointF(30, 330));
            graphics.DrawString(remainMeter.ToString(), largeFont, Brushes.White, new PointF(160, 320));
            graphics.DrawString("停次:", mediumFont, Brushes.Yellow, new PointF(500, 260));
            graphics.DrawString(stopNum.ToString(), largeFont, Brushes.White, new PointF(630, 250));
            graphics.DrawString("停时:", mediumFont, Brushes.Yellow, new PointF(500, 330));
            graphics.DrawString(stopTime.ToString(), largeFont, Brushes.White, new PointF(630, 320));

            // 备注
            graphics.DrawString($"备注:{remark}", smallFont, Brushes.Red, new PointF(30, 450));

            // 释放资源
            graphics.Dispose();

            pictureBox.Image = bitmap;
        }

        private static void DrawSpeedometer(Graphics g, int width, int height, int maxSpeed = 420, int speed1 = 100, int speed2 = 300, int speed3 = 200)
        {
            int radius = width / 2; //半径

            // 绘制半圆背景
            g.FillPie(Brushes.Black, new Rectangle(60, 60, width, height), 180, 180);

            double centerX = (double)width / 2 + 60; //中心点X坐标

            double centerY = (double)height / 2 + 60; //中心点Y坐标

            // 绘制数字标记
            Font font = new Font("Arial", 18, FontStyle.Bold);
            for (int i = 0; i <= maxSpeed; i += 50)
            {
                if (i > 300) continue;

                double angle = i / (double)maxSpeed * 180;
                double radian = angle * Math.PI / 180;
                int x = (int)(centerX - (radius + 35) * Math.Cos(radian));
                int y = (int)(centerY - (radius + 20) * Math.Sin(radian));

                g.DrawString(i.ToString(), font, Brushes.White, x - 10, y - 25);
            }
            float speedAngle1 = speed1 / (float)maxSpeed * 180;
            float speedAngle2 = speed2 / (float)maxSpeed * 180;

            float speedAngle3 = speed3 / (float)maxSpeed * 180;

            Pen redPen = new Pen(Color.Red, 21);
            Pen yellowPen = new Pen(Color.Yellow, 21);
            Pen greenPen = new Pen(Color.SpringGreen, 20);

            int radius1 = radius - 10 + 1;
            int radius2 = radius - 30 + 1;
            int radius3 = radius - 50 + 1;

            g.DrawArc(redPen, (int)centerX - radius1, (int)centerY - radius1, radius1 * 2, radius1 * 2, 180, speedAngle1);

            g.DrawArc(yellowPen, (int)centerX - radius2, (int)centerY - radius2, radius2 * 2, radius2 * 2, 180, speedAngle2);

            g.DrawArc(greenPen, (int)centerX - radius3, (int)centerY - radius3, radius3 * 2, radius3 * 2, 180, speedAngle3);


            //Pen blackPen = new Pen(Color.Black, 5);
            //g.DrawLine(blackPen, new Point(50, 220), new Point(400, 220));
        }

    }
}
